package com.five.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.dto.*;
import com.five.admin.server.AdminService;
import com.five.admin.server.RoleService;
import com.five.admin.threadLocal.AdminThreadLocal;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.pojo.dto.BatchIds;
import com.five.pojo.entity.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.PublicKey;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;


    /**
     * 管理员登录
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result adminLogin(@RequestBody @Valid AdminLoginDto adminLoginDto){
        log.info("接收到登录请求: {}", adminLoginDto);
        System.out.println("adminLoginDto = " + adminLoginDto);
        String token = adminService.adminLogin(adminLoginDto);
        return Result.buildSuccess(token);
    }
    /**
     * 获取当前登录用户信息
     * @return
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public Result getInfo(){
        AdminThreadLocalDto adminThreadLocalDto = AdminThreadLocal.get();
        return Result.buildSuccess(adminThreadLocalDto);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody AdminEditDto adminEditDto){
        boolean saved = adminService.addAdmin(adminEditDto);
        return saved?Result.buildSuccess():Result.buildFail(ResultEnum.ADD_FALSE);
    }

    @RequestMapping(value = "/queryOne/{id}", method = RequestMethod.GET)
    public Result queryOne(@PathVariable("id") Long id){
        Admin admin1 = adminService.getById(id);
        return Result.buildSuccess(admin1);
    }
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody AdminEditDto adminEditDto){
        adminService.updateAdmin(adminEditDto);
        return Result.buildSuccess();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Long id){
        boolean deleted = adminService.deleteAdmin(id);
        return deleted?Result.buildSuccess():Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result deleteBatch(@RequestBody @Valid BatchIds batchIds){
        if (adminService.removeByIds(batchIds.getIds())) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/allocateRoles", method = RequestMethod.POST)
    public Result allocateRoles(@RequestBody @Valid AllocateDto allocateDto){
        adminService.allocateRoles(allocateDto);
        return Result.buildSuccess();
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestBody AdminQueryDto adminQueryDto){
        Page<Admin> page = new Page<>(adminQueryDto.getPage(), adminQueryDto.getLimit());
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like((adminQueryDto.getUsername()!=null && !adminQueryDto.getUsername().isEmpty()),Admin::getUsername, adminQueryDto.getUsername());
        Page<Admin> adminPage = adminService.page(page, queryWrapper);
        return Result.buildSuccess(adminPage.getTotal(), adminPage.getRecords());
    }
}
